package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.AntiepidemicPurc;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.VaccineInfor;
import com.suyuan.entity.Vaccoutstroe;
import com.suyuan.service.CompanyInfoService;
import com.suyuan.service.VaccInStoreService;
import com.suyuan.service.VaccOutStoreService;
import com.suyuan.service.VaccineInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 疫苗出库控制器(物资管理部分)
 */
@Controller
@RequestMapping(value="/admin/vaccOutStore")
public class VaccOutStoreController {
    @Resource(name = "vaccOutStoreService")
    private VaccOutStoreService vaccOutStoreService;

    @Resource(name = "vaccineInfoService")
    private VaccineInfoService vaccineInfoService;

    @Resource(name = "companyInfoService")
    private CompanyInfoService companyInfoService;

    @RequestMapping(value = "/list")
    public String list(Integer vaccineId, String outDate, String remark, Page page,ModelMap model){
        //查询条件(出库疫苗种类)需要用到的信息，包括后面的list_coms
        List<VaccineInfor> list_vacs=vaccineInfoService.findAll();
        model.addAttribute("list_vacs",list_vacs);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date out = null;
        if(outDate!=null && !"".equals(outDate.trim())){
            try {
                out = sf.parse(outDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //避免即使不输入出库人controller层还是接收到""而不是null这种情况发生
        if(remark!=null && "".equals(remark.trim())){
            remark=null;
        }
        //避免即使不选择出库疫苗种类controller层还是接收到""而不是null这种情况发生
//        if(vaccineId!=null && "".equals(vaccineId.trim())){
//            vaccineId=null;
//        }
        System.out.println("获取的id为>>>>>>>>>>>>"+vaccineId);
        PageInfo info =vaccOutStoreService.inquiry(vaccineId,out,remark,page);

        //查询所有的疫苗信息，用于显示疫苗名称和供应商信息
        List<VaccineInfor> vaccineInfors=vaccineInfoService.findAll();
        //查询所有的供应商信息，用于显示供应商名称
        List<CompanyInfo> list_coms=companyInfoService.findAll();
        model.addAttribute("list_coms",list_coms);
        model.addAttribute("vaccineInfors",vaccineInfors);
        model.addAttribute("page",info);
        return "vaccOutStore_list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping( value = "add")
    public String add(ModelMap model){
        List<VaccineInfor> list_vac=vaccineInfoService.findAll();
        List<CompanyInfo> list_com=companyInfoService.findAll();
        model.addAttribute("list_vac",list_vac);
        model.addAttribute("list_com",list_com);
        return "vaccOutStore_add";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping( value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(Vaccoutstroe vaccoutstroe){
        int code = 0;
        try {
            //通过选中的需要出库的疫苗种类查询它原来的库存信息，以便新增疫苗出库信息时修改库存量
            int oldNumOutStore=vaccineInfoService.findByVaccineId(vaccoutstroe.getVaccineid()).getNuminstore();
            code = vaccOutStoreService.save(vaccoutstroe,oldNumOutStore);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping( value = "delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = vaccOutStoreService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            vaccOutStoreService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 跳到编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer outStoreId,ModelMap model){
        //查找需要编辑的疫苗出库信息做数据回显
        Vaccoutstroe vaccoutstroe=vaccOutStoreService.findByOutStoreId(outStoreId);
        //查找所有的疫苗信息以便修改时选择
        List<VaccineInfor> list_vac=vaccineInfoService.findAll();
        //查找所有的供应商信息显示供应商名称而不是id
        List<CompanyInfo> list_com=companyInfoService.findAll();
        model.addAttribute("vaccOutStore",vaccoutstroe);
        model.addAttribute("list_vac",list_vac);
        model.addAttribute("list_com",list_com);
        return "vaccOutStore_edit";
    }

    @RequestMapping( value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(Vaccoutstroe vaccoutstroe,Integer oldOutStoreNum){
        int code = 0;
        try {
            System.out.println("获取的原来入库数量为："+oldOutStoreNum);
            code = vaccOutStoreService.update(vaccoutstroe,oldOutStoreNum);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }
}
